
#include "../headers/SALES.h"

void VIP_manager() {
    while (1) {
        pointer *target;
        int choice, counter = 0;
        print_menu("[1]查询或修改会员信息\t[2]添加新会员\t[3]返回上一级");
        while (!input_int(&choice, 1, 3));
        switch (choice) {
            case 1:
                print_menu("[1]打印全部会员信息\t[2]根据会员名（电话号码）查找：");
                if (!input_int(&choice, 1, 2))
                    break;
                pointer *p = pointer_create();
                if (choice == 1)
                    for (account *i = head_VIP->next; i != NULL; i = i->next) {
                        pointer_add(p, i, 3, i->money);
                        counter++;
                    }
                if (choice == 2) {
                    char username[64];
                    printf("请输入会员名（）电话号码：");
                    while (!input_string(username, 64));
                    for (account *i = head_VIP->next; i != NULL; i = i->next) {
                        if (strcmp(username, i->username) == 0) {
                            pointer_add(p, i, 3, i->money);
                            counter++;
                        }
                    }
                }
                if (p->next == NULL) {
                    puts("没有找到相关会员!");
                    system_pause();
                    return;
                }
                print_VIP(p);
                printf("请选择您要修改的用户序号（Enter返回）：");
                if (!input_int(&choice, 1, counter))
                    break;
                target = p;
                while (counter--)
                    target = target->next;
                while (!VIP_modify(target->p_account));
                break;
            case 2:
                while (!VIP_register());
                break;
            case 3:
                return;
        }
    }
}


bool VIP_register() {//会员注册
    char password_confirm[64];
    account VIP;
    account *p;
    printf("请依次输入您要添加的会员信息。\n");
    printf("请输入您的会员名（电话号码）：");
    while (!input_string(VIP.username, 64));
    printf("请输入您的密码：");
    while (!input_string(VIP.password, 64));
    printf("请确认您的密码：");
    while (!input_string(password_confirm, 64));
    if (strcmp(VIP.password, password_confirm) != 0) {
        printf("🥺 对不起，您两次输入的密码不一致！\n");
        printf("正在返回注册界面，请你重新进行操作。\n");
        return false;
    }
    for (p = head_VIP->next; p != NULL; p = p->next)
        if (strcmp(VIP.username, p->username) == 0)
            break;
    if (p == NULL) {
        printf("😄 注册成功！");
        get_MD5(VIP.password);
        get_timeItem(&VIP.addTime);
        VIP.money = 0;
        VIP.type = VIP_level(&VIP);
        printf("您注册会员的时间为%d年%d月%d日。\n", VIP.addTime.year, VIP.addTime.month, VIP.addTime.day);
        printf("从现在开始，您的每一笔消费金额都会被记录在您的会员账户中（注册前的消费金额将不会被记录）。\n");
        account_add(head_VIP, VIP);
        return true;
    } else {
        printf("🥺 对不起，您输入的会员名（电话号码）已经被注册！\n");
        printf("正在返回注册界面，请你重新进行操作。\n");
        return false;
    }
}

char VIP_level(account *p) {//根据其消费金额来生成会员等级
    int level;
    level = p->money / 2000;
    if (level <= 2)
        return 'E';
    else if (level <= 4)
        return 'D';
    else if (level <= 6)
        return 'C';
    else if (level <= 8)
        return 'B';
    else if (level <= 10)
        return 'A';
    else
        return 'S';
}

account *VIP_login() {//会员登陆
    char username[64], password[64];
    account *p = head_VIP;
    printf("正在连接到会员登陆系统······\n");
    while (1) {
        printf("请输入您的会员名（电话号码）：");
        while (!input_string(username, 64));
        printf("请输入您的密码：");
        while (!input_string(password, 64));
        get_MD5(password);
        while (p->next != NULL) {
            p = p->next;
            if (strcmp(p->username, username) == 0) {
                if (strcmp(p->password, password) == 0) {
                    printf("😄 登陆成功！");
                    printf("欢迎您，尊敬的Lv.%c会员！\n", p->type);
                    return p;
                }
            }
        }
        printf("🥺 对不起，您输入的用户名或密码错误！\n");
        print_menu("[1]重新登陆\t[2]放弃登录");
        int choice;
        while (!input_int(&choice, 1, 2));
        if (choice == 2) return NULL;
    }
}

bool VIP_modify(account *p) {//修改会员信息
    int option;
    char username_new[64], password[64], password_confirm[64];
    printf("请选择您要修改的信息：\n");
    print_menu("[1]会员名（电话号码）\t[2]密码\t[3]删除\t[4]取消修改");
    while (!input_int(&option, 1, 4));
    if (option == 4) return true;
    switch (option) {
        case 1:
            printf("请输入您的新会员名（电话号码）：");
            while (!input_string(username_new, 64));
            for (account *i = head_VIP->next; i != NULL; i = i->next) {
                if (strcmp(username_new, i->username) == 0) {
                    printf("🥺 对不起，您输入的会员名（电话号码）已经被注册！\n");
                    printf("正在返回修改界面，请您重新进行操作。\n");
                    return false;
                }
            }
            strcpy(p->username, username_new);
            printf("😄 修改成功！\n");
            return true;
        case 2:
            printf("请输入您的新密码:");
            while (!input_string(password, 64));
            printf("请确认您的新密码:");
            while (!input_string(password_confirm, 64));
            if (strcmp(password, password_confirm) == 0) {
                strcpy(p->password, password);
                get_MD5(p->password);
                printf("😄 修改成功！\n");
                return true;
            } else {
                printf("🥺 对不起，您两次输入的密码不一致！");
                printf("正在返回修改界面，请你重新进行操作。\n");
                return false;
            }
        case 3:
            printf("确认删除?[1]是\t[2]否:");
            while (!input_int(&option, 1, 2));
            if (option == 1)
                account_del(head_VIP, p);
            else
                printf("删除已取消，正在返回上一级······\n");
            return true;
        default:
            printf("🥺 对不起，您不能修改其他信息\n");
            printf("正在返回修改界面，请你重新进行操作。\n");
            return false;
    }
}

double VIP_discount(account *p) {
    double discount[100];
    discount['E'] = 0.99;
    discount['D'] = 0.95;
    discount['C'] = 0.90;
    discount['B'] = 0.88;
    discount['A'] = 0.85;
    discount['S'] = 0.80;
    return discount[p->type];
}

void pay(account *VIP, double money_total) {//支付环节
    int choice;
    double money_pay = 0, temp;

    if (money_total == 0) {
        printf("购物车是空的，无需支付！\n");
        return;
    }

    if (VIP != NULL) {
        printf("顾客为Lv.%c会员，将对总价进行%.1lf折处理\n", VIP->type, VIP_discount(VIP) * 10);
        printf("已优惠%.2lf元,", money_total - money_total * VIP_discount(VIP));
        money_total *= VIP_discount(VIP);
    }

    while (1) {
        printf("还需要支付人民币：%.2lf元\n", money_total - money_pay);
        printf("请选择支付方式：\n");
        print_menu("[1]现金\t[2]银行卡\t[3]支付宝\t[4]微信支付\t[5]取消支付");
        char way[5][64] = {"", "现金", "银联刷卡", "支付宝扫码", "微信扫码"};
        while (!input_int(&choice, 1, 5));
        printf("等待%s支付……", way[choice]);
        fflush(stdout);
        system_pause();
        printf("请输入支付金额（Enter取消）：");
        if (!input_double(&temp)) continue;
        money_pay += temp;
        if (money_pay >= money_total) {
            if (money_pay > money_total) {
                printf("支付了%.2lf元，但总价为%.2lf元\n", money_pay, money_total);
                puts("需要凑整吗？");
                print_menu("[1]是\t[2]否");
                while (!input_int(&choice, 1, 2));
                if (choice == 1) puts("凑整操作完成！");
                if (choice == 2) {
                    printf("请找零%.2lf元……\n", money_pay - money_total);
                    money_pay = money_total;
                }
            }
            puts("支付成功！感谢惠顾！");
            current_user->money += money_pay;
            if (VIP != NULL) {
                puts("会员积分操作完成");
                VIP->money += money_pay;
                if (VIP_level(VIP) != VIP->type) {
                    VIP->type = VIP_level(VIP);
                    printf("恭喜您升级为Lv.%c会员！\n", VIP->type);
                }
            }
            return;
        }
    }
}

void check_out() {
    int choice;
    double totalMoney = 0;
    account *VIP_check_out = NULL;
    fruit *goods = fruit_create();
    pointer *sync = pointer_create();   //同步指针链表

    printf("🤔 顾客是否是会员？\n");
    print_menu("[1]是\t[2]否");
    while (!input_int(&choice, 1, 2));
    if (choice == 1) VIP_check_out = VIP_login();
    if (choice == 2) {
        puts("🤔 顾客是否注册会员？");
        print_menu("[1]是\t[2]否");
        while (!input_int(&choice, 1, 2));
        if (choice == 1) {
            VIP_register();
            VIP_check_out = VIP_login();
        }
    }

    if (VIP_check_out != NULL) {
        puts("会员账户信息如下：");
        pointer *temp = pointer_create();
        pointer_add(temp, VIP_check_out, 3, 0);
        print_VIP(temp);
        puts("🤔 要修改会员信息吗？");
        print_menu("[1]是\t[2]否");
        while (!input_int(&choice, 1, 2));
        if (choice == 1) {
            while (!VIP_modify(VIP_check_out));
            puts("新的会员账户信息如下：");
            print_VIP(temp);
        }
    }

    while (1) {
        printf("请选择您的操作：\n");
        print_menu("[1]添加商品至收银台\t[2]查看与修改已添加商品\t[3]支付\t[4]取消收银");
        while (!input_int(&choice, 1, 4));
        switch (choice) {
            case 1: {
                pointer *temp = pointer_create();
                printf("请选择查找方式：\n");
                temp = shelf_find(head_fruitOnShelf, temp); //调用理货员的函数完成查找
                choice = 0;
                for (pointer *j = temp->next; j != NULL; j = j->next, choice++);
                if (!choice) continue;
                printf("请选择您要添加的商品序号：");
                while (!input_int(&choice, 1, choice));
                while (choice--) temp = temp->next;
                fruit target = *(temp->p_fruit);
                printf("请输入购买数量：");
                if (target.kilo > temp->p_fruit->kilo) {
                    printf("😥 您输入的数量超过了库存量！");
                    break;
                }
                while (!input_double(&target.kilo));
                fruit_add(goods, target);
                pointer_add(sync, temp->p_fruit, 0, 0);
                totalMoney += target.unitPrice * target.kilo;
                printf("😄 添加成功！");
                free(temp);
                break;
            }
            case 2: {
                pointer *temp = pointer_create();
                for (fruit *i = goods->next; i != NULL; i = i->next, choice++)
                    pointer_add(temp, i, 0, 0);
                print_fruitOnShelf(temp);
                printf("🤔 要修改吗？Enter退出或输入要修改的编号：");
                if (!input_int(&choice, 1, choice)) break;
                pointer *target_pointer = temp;
                fruit *target_fruit = goods;
                for (; choice--; target_fruit = target_fruit->next, target_pointer = target_pointer->next);
                puts("请选择修改操作：");
                print_menu("[1]修改数量\t[2]删除商品\t[3]取消修改");
                while (!input_int(&choice, 1, 3));
                switch (choice) {
                    case 1:
                        totalMoney -= target_fruit->unitPrice * target_fruit->kilo;
                        printf("请输入新的数量：");
                        while (!input_double(&target_fruit->kilo));
                        totalMoney += target_fruit->unitPrice * target_fruit->kilo;
                        printf("😄 修改成功！");
                        break;
                    case 2:
                        pointer_del(sync, target_pointer);
                        fruit_del(goods, target_fruit);
                        printf("😄 删除成功！");
                        break;
                    case 3:
                        break;
                }
                break;
            }
            case 3: {
                pay(VIP_check_out, totalMoney);
                fruit *i = goods->next;
                pointer *j = sync->next;
                while (i != NULL && j != NULL) {
                    j->p_fruit->kilo -= i->kilo;
                    if (j->p_fruit->kilo <= 0.1)    //如果库存为低于阈值，则删除该商品
                        fruit_del(head_fruitOnShelf, j->p_fruit);
                    i = i->next;
                    j = j->next;
                }
                sellMessage temp;
                strcpy(temp.customer, VIP_check_out == NULL ? "GUEST" : VIP_check_out->username);
                get_timeItem(&temp.sellTime);
                temp.totalPrice = totalMoney;
                temp.number = 0;
                for (fruit *p = goods->next; p != NULL; p = p->next, temp.number++);
                temp.head = goods;
                sellMessage_add(head_sellMessage, temp);
                pointer_free(sync);
                system_pause();
                return;
            }
            case 4:
                fruit_free(goods);
                pointer_free(sync);
                return;
        }
    }
}

void wholesale_system() {
    int choice;
    double totalMoney = 0;
    pointer *sync = pointer_create();
    fruit *goods = fruit_create();
    while (1) {
        printf("请选择您的操作：\n");
        print_menu("[1]添加货物至暂存区\t[2]查看与修改已添加货物\t[3]完成交易\t[4]取消交易");
        while (!input_int(&choice, 1, 4));
        switch (choice) {
            case 1: {
                pointer *temp = pointer_create();
                printf("请选择查找方式：\n");
                temp = storage_find(head_fruitStored, temp); //调用理货员的函数完成查找
                choice = 0;
                for (pointer *j = temp->next; j != NULL; j = j->next, choice++);
                if (!choice) continue;
                printf("请选择您要添加的货物序号：");
                while (!input_int(&choice, 1, choice));
                while (choice--) temp = temp->next;
                fruit target = *(temp->p_fruit);
                printf("请输入购买数量：");
                while (!input_double(&target.kilo));
                if (target.kilo > temp->p_fruit->kilo) {
                    printf("😥 您输入的数量超过了库存量！");
                    break;
                }
                target.mode = wholesale;
                target.level = good;
                get_timeItem(&target.shelfTime);
                fruit_add(goods, target);
                pointer_add(sync, temp->p_fruit, 0, 0);
                totalMoney += target.unitPrice * target.kilo;
                printf("😄 添加成功！");
                free(temp);
                break;
            }
            case 2: {
                pointer *temp = pointer_create();
                for (fruit *i = goods->next; i != NULL; i = i->next, choice++)
                    pointer_add(temp, i, 0, 0);
                print_fruitStored(temp);
                printf("🤔 要修改吗？Enter退出或输入要修改的编号：");
                if (!input_int(&choice, 1, choice)) break;
                pointer *target_pointer = temp;
                fruit *target_fruit = goods;
                for (; choice--; target_pointer = target_pointer->next, target_fruit = target_fruit->next);
                puts("请选择修改操作：");
                print_menu("[1]修改数量\t[2]删除货物\t[3]取消修改");
                while (!input_int(&choice, 1, 3));
                switch (choice) {
                    case 1:
                        totalMoney -= target_fruit->unitPrice * target_fruit->kilo;
                        printf("请输入新的数量：");
                        while (!input_double(&target_fruit->kilo));
                        totalMoney += target_fruit->unitPrice * target_fruit->kilo;
                        printf("😄 修改成功！");
                        break;
                    case 2:
                        pointer_del(sync, target_pointer);
                        fruit_del(goods, target_fruit);
                        printf("😄 删除成功！");
                        break;
                    case 3:
                        break;
                }
                pointer_free(temp);
                break;
            }
            case 3: {
                if (totalMoney == 0) {
                    puts("无需支付！");
                    return;
                }
                printf("需要支付的金额为：%.2lf元\n", totalMoney);
                printf("正在等待付款……");
                //system_pause();
                printf("请输入实收金额：");
                double money_pay;
                while (!input_double(&money_pay));
                printf("支付成功！\n");
                current_user->money += money_pay;
                fruit *i = goods->next;
                pointer *j = sync->next;
                while (i != NULL && j != NULL) {
                    j->p_fruit->kilo -= i->kilo;
                    if (j->p_fruit->kilo <= 0.1)    //如果库存为低于阈值，则删除该商品
                        fruit_del(head_fruitStored, j->p_fruit);
                    i = i->next;
                    j = j->next;
                }
                printf("请输入批发人：");
                sellMessage temp;
                while (!input_string(temp.customer, 64));
                get_timeItem(&temp.sellTime);
                temp.totalPrice = totalMoney;
                temp.number = 0;
                for (fruit *p = goods->next; p != NULL; p = p->next, temp.number++);
                temp.head = goods;
                sellMessage_add(head_sellMessage, temp);
                pointer_free(sync);
                puts("😄 批发成功！");
                system_pause();
                return;
            }
            case 4:
                fruit_free(goods);
                pointer_free(sync);
                return;
        }
    }
}
